package edu.whu.puppygo.dao;

import edu.whu.puppygo.entity.PetMatter;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author xinjiezhuang
 * @Date 2023-11-20
 */
@Repository
public interface PetMatterDao extends JpaRepository<PetMatter, Long>, JpaSpecificationExecutor<PetMatter> {
    @Query("select p from PetMatter p where p.userId = ?1 and p.deleteTime is null")
    List<PetMatter> findByUserIdAndDeleteTimeNull(Long userId, Pageable pageable);
//    SELECT * FROM `pet_matter` as p
//    WHERE p.user_id = 8
//    AND p.delete_time IS NULL
//    AND CURRENT_DATE >= p.start_time
//    AND CURRENT_DATE <= p.finish_time
//    AND MOD(DATEDIFF(CURRENT_DATE, p.start_time), p.implementation_cycle_by_day) = 0
@Query("SELECT p FROM PetMatter p " +
        "WHERE p.userId = ?1 " +
        "AND p.deleteTime IS NULL " +
        "AND CURRENT_DATE >= p.startTime " +
        "AND CURRENT_DATE <= p.finishTime " +
        "AND FUNCTION('MOD', FUNCTION('DATEDIFF', CURRENT_DATE(), p.startTime), p.implementationCycleByDay) = 0")
    List<PetMatter> findTodayPetMatters(Long userId, Pageable pageable);

    @Query("select p from PetMatter p where p.deleteTime is null and p.userId = ?1 order by p.updateTime DESC")
    List<PetMatter> findByDeleteTimeNullAndUserIdOrderByUpdateTimeDesc(Long userId);

    @Query("select p from PetMatter p where p.deleteTime is not null and p.userId = ?1 order by p.updateTime DESC")
    List<PetMatter> findByDeleteTimeNotNullAndUserIdOrderByUpdateTimeDesc(Long userId);

    @Query("select p from PetMatter p where p.petId = ?1 order by p.createTime DESC")
    List<PetMatter> findByPetIdOrderByCreateTimeDesc(Long petId);


}
